package com.ly.honor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ly.honor.entity.GoodsAppraise;
import com.ly.honor.entity.GoodsOrder;
import com.ly.honor.entity.User;
import com.ly.honor.entity.dto.GoodsAppraiseCreateDTO;
import com.ly.honor.entity.vo.GoodsAppraiseQueryVO;
import com.ly.honor.mapper.GoodsOrderMapper;
import com.ly.honor.service.GoodsAppraiseService;
import com.ly.honor.mapper.GoodsAppraiseMapper;
import com.ly.honor.util.RedisUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* @author 17798
* @description 针对表【bs_goods_appraise】的数据库操作Service实现
* @createDate 2023-04-05 00:28:22
*/
@Service
public class GoodsAppraiseServiceImpl extends ServiceImpl<GoodsAppraiseMapper, GoodsAppraise>
    implements GoodsAppraiseService{

    @Autowired
    private GoodsAppraiseMapper goodsAppraiseMapper;

    @Autowired
    private GoodsOrderMapper goodsOrderMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Page<GoodsAppraiseQueryVO> getGoodsAppraisePage(Integer pageNum, Integer pageSize,Integer goodsId) {
        var page = new Page<GoodsAppraiseQueryVO>(pageNum, pageSize);
        Page<GoodsAppraiseQueryVO> goodsAppraiseQueryVOPage = goodsAppraiseMapper.getGoodsAppraisePage(page,goodsId);
        for (GoodsAppraiseQueryVO goodsAppraiseQueryVO : goodsAppraiseQueryVOPage.getRecords()){
            var id = goodsAppraiseQueryVO.getId();
            List<GoodsAppraiseQueryVO> goodsAppraiseQueryVOList = goodsAppraiseMapper.getGoodsAppraiseByParentId(id);
            goodsAppraiseQueryVO.setGoodsAppraiseQueryVOList(goodsAppraiseQueryVOList);
        }
        return goodsAppraiseQueryVOPage;
    }

    @Override
    public Integer getGoodsAppraiseCount(Integer goodsId) {
        var wrapper = new LambdaQueryWrapper<GoodsAppraise>();
        wrapper.eq(GoodsAppraise::getGoodsId,goodsId);
        var goodsAppraiseCount = goodsAppraiseMapper.selectCount(wrapper);
        return goodsAppraiseCount;
    }

    @Override
    public void addGoodsAppraise(GoodsAppraiseCreateDTO goodsAppraiseCreateDTO, HttpServletRequest request) {
        var token = request.getHeader("X-Token");
        var userInfo = redisUtil.get(token);
        var objectMapper = new ObjectMapper();
        var user = objectMapper.convertValue(userInfo, User.class);
        goodsAppraiseCreateDTO.setUserId(user.getId());
        goodsAppraiseCreateDTO.setLevel(2);
        var goodsAppraise = new GoodsAppraise();
        BeanUtils.copyProperties(goodsAppraiseCreateDTO,goodsAppraise);
        goodsAppraiseMapper.insert(goodsAppraise);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOneAppraise(GoodsAppraiseCreateDTO goodsAppraiseCreateDTO, HttpServletRequest request) {
        var token = request.getHeader("X-Token");
        var userInfo = redisUtil.get(token);
        var objectMapper = new ObjectMapper();
        var user = objectMapper.convertValue(userInfo, User.class);
        goodsAppraiseCreateDTO.setUserId(user.getId());
        goodsAppraiseCreateDTO.setLevel(1);
        var goodsAppraise = new GoodsAppraise();
        BeanUtils.copyProperties(goodsAppraiseCreateDTO,goodsAppraise);
        goodsAppraiseMapper.insert(goodsAppraise);

        var wrapper = new LambdaQueryWrapper<GoodsOrder>();
        wrapper.eq(GoodsOrder::getOrderNumber,goodsAppraiseCreateDTO.getOrderNumber());
        GoodsOrder goodsOrder = goodsOrderMapper.selectOne(wrapper);

        goodsOrder.setOrderStatusId(5);
        goodsOrderMapper.updateById(goodsOrder);

    }
}




